<?php

namespace app\admin\controller;

use think\Controller;
use app\admin\model\Config as cf;
use think\facade\Request as Req;

class Config extends Auth
{


      // 获取某个标签的配置参数
    public function group() {
        $map['state']=1;
        $map['group']=$id=input('id',1);
        $type   =   config('CONFIG_GROUP_LIST');
        $list   =   model("config")->where($map)->field('id,name,title,extra,value,remark,type')->order('sort DESC')->select();
        if($list)$this->assign('list',$list);
        return view("",compact('type','id'));
    }

    /**
     * 批量保存配置
     */
    public function save($config,cf $conf){
        if($config && is_array($config)){
            foreach ($config as $name => $value) {
                $map = array('name' => $name);
                $conf->where($map)->setField('value', $value);
            }
        }
        cache('DB_CONFIG_DATA',null);
        $this->success('保存成功！');
    }


    //配置数据管理
    //*+------------------------------------------------------------------------+*/


    //配置管理
    public function index(){
    	//按状态查询
        $state=input('state',1);
    	if($state>-1)$map[]=['state','=',$state];

    	//按分组查询
    	$group=input('group',0);
        if($group)$map[]=['group','=',$group];

        //按分类查询
        $type=input('type',0);
        if($type)$map[] = ['type','=',$type];

        //按关键词查询
        $keyword=input('keyword','','trim');
        if($keyword)$map[] = ['name|title','like','%'.$keyword.'%'];

        $pagesize=20;
        $list = db("config")->where($map)->order("sort desc")->paginate($pagesize,false,['query'=>request()->param()]);

        //状态
        $reviewary=array(-1=>'全部',1=>'启用',0=>'禁用');
        return view("",compact(['list','reviewary','keyword','type','group','state','pages']));
    }


    //添加编辑配置参数
    public function edit(){
    	$id=input('id',0);

        if(request()->isPost()){
            $data=request()->post();
            $result = $this->validate($data ,'app\admin\validate\ConfigValidate');
            if($result !== true)$this->error($result);
            $data['name']=strtoupper($data['name']);

            cache('DB_CONFIG_DATA',null);
            $this->editDataMethod($data);
        }

        if($id){
	        $map['id']=$id;
	        $info = db("config")->where($map)->find();
	        $this->assign('info', $info);
        }

        return view();
    }

}
